package com.wgj.android.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiaai.xui.adapter.decoration.DividerDecoration;
import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.jiaai.xui.view.widget.loadingview.XLoadingView;
import com.wgj.android.R;
import com.wgj.android.adapter.BankAdapter;
import com.wgj.android.entity.BankInfo;
import com.wgj.android.entity.NewHistory;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.CacheUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyBankActivity extends AppCompatActivity  implements SwipeRefreshLayout.OnRefreshListener {
    private List<BankInfo> bankInfos = new ArrayList<BankInfo>();
    @BindView(R.id.loadingView)
    XLoadingView loadingView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.banckListView)
     RecyclerView banckListView;
    BankAdapter bankAdapter;

    public  static  void  openMyBankUI(Context context){
        Intent intent=new Intent(context,MyBankActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_bank);
        ButterKnife.bind(this);
        initView();

    }

    private void initView() {
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));
        bankAdapter=new BankAdapter(banckListView,bankInfos);
        banckListView.setLayoutManager( new LinearLayoutManager(this));
        banckListView.setHasFixedSize(true);
        banckListView.setItemAnimator(new DefaultItemAnimator());
        banckListView.setAdapter(bankAdapter);
    }



    @Override
    protected void onResume() {
        refresh();
        swipeRefreshLayout.setRefreshing(true);
        super.onResume();
    }

    @OnClick(R.id.add)
    public void onAddBank(View v){
        Intent intent = new Intent(this, BindActivity.class);
        startActivity(intent);
    }


    @Override
    public void onRefresh() {
        refresh();
    }

    private void refresh() {
        Map<String,Object> params=new HashMap<>();
        String str =  CacheUtils.getMember().getM_id();
        params.put("mId", CacheUtils.getMember().getM_id());
        XHttp.obtain().get(WGJAPI.FIND_BANK_URL, params, new ListHttpCallBack<BankInfo>() {

            @Override
            public void onSuccess(List<BankInfo> list) {
                swipeRefreshLayout.setRefreshing(false);
                if(list==null ||list.size()==0){
                        loadingView.showEmpty("亲,你还没有绑定银行卡哦");
                }else {
                    bankInfos.clear();
                    bankInfos.addAll(list);
                    bankAdapter.notifyDataSetChanged();
                    loadingView.showContent();
                }


            }
            @Override
            public void onFailed(int i, String s, boolean b) {
                swipeRefreshLayout.setRefreshing(false);
                if(b){
                    loadingView.showNoNetwork();
                }else {
                    loadingView.showError();
                }
            }
        });
    }
}
